﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MR_HKSUtility;

namespace MR_HKSCalc
{
    /**
     * Multimedia Retrieval
     * Heat Kernel Signature project
     * Frank Borgonjen, Joeri van der Lei & Kevin van der Lei
     * 28-10-2011
     * 
     * The ComparisonCalculator class is used to construct for a certain model a list containing
     * all other models within the distance matrix in order of distance. This list is saved in .csv
     * format so it can easily be examined.
     */
    class ComparisonCalculator
    {
        #region Functions

        /**
         * This method calculates a list with the distances to all other models for each model whose name
         * matches the given string parameter. For instance, in order to generate lists for all unaltered
         * models, the string "null" should be chosen. For all models in group 3, the string "0003" should be
         * chosen.
         * @param matrixFileName the full path name of the matrix file that contains all distances 
         * between models.
         * @param match a string that will be matched with the model names to determine whether or not
         * an individual distance file should be generated.
         */
        public static void CalculateComparisonLists(string matrixFileName, string match)
        {
            TextReader reader = new StreamReader(matrixFileName);

            //read first line (model names) for storage
            string[] splitNames = (reader.ReadLine()).Split(",".ToCharArray());

            string line = reader.ReadLine();
            while (line != null)
            {
                string[] splitLine = line.Split(",".ToCharArray());
                if (splitLine[0].ToLower().Contains(match))
                {
                    // make a list of all entries
                    Dictionary<string, double> dict = new Dictionary<string, double>();
                    for(int i = 1; i < splitLine.Length; i++)
                    {
                        // pairs are defined as <name, distance value>
                        Double d;
                        Double.TryParse(splitLine[i].Replace('.',','), out d);
                        dict.Add(splitNames[i], d);
                    }

                    // sort all entries using a custom sorting method to compare the distance values
                    List<KeyValuePair<string, double>> sortedList = new List<KeyValuePair<string, double>>(dict);
                    sortedList.Sort(
                        delegate(KeyValuePair<string, double> first,
                        KeyValuePair<string, double> second)
                        {
                            return first.Value.CompareTo(second.Value);
                        }
                    );

                    // store the sorted list
                    StreamWriter sw = new StreamWriter(Path.GetFileNameWithoutExtension(matrixFileName) + "_" + splitLine[0] + ".csv");
                    for (int i = 0; i < splitLine.Length - 1; i++)
                    {
                        sw.WriteLine(sortedList[i].Key + ", " + (sortedList[i].Value).ToString().Replace(',','.'));
                    }
                    sw.Flush();
                    sw.Close();
                }
                // read a new line
                line = reader.ReadLine();
            }

            reader.Close();
        }

        #endregion
    }
}
